<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>IVT绩效考核系统管理后台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
            padding: 40px;
            width: 400px;
            max-width: 90%;
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .logo {
            font-size: 48px;
            margin-bottom: 10px;
        }

        .title {
            font-size: 24px;
            font-weight: 600;
            color: #333;
            margin-bottom: 5px;
        }

        .subtitle {
            font-size: 14px;
            color: #666;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            font-weight: 500;
            color: #333;
        }

        .form-input {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }

        .form-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .login-btn {
            width: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 6px;
            padding: 14px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 20px;
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
        }

        .login-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .error-message {
            background: #fee;
            color: #c33;
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            text-align: center;
            display: none;
        }

        .error-message.show {
            display: block;
        }

        .footer {
            text-align: center;
            margin-top: 20px;
            font-size: 12px;
            color: #999;
        }

        /* 验证码样式 */
        .captcha-container {
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .captcha-input {
            flex: 1;
        }

        .captcha-image {
            width: 120px;
            height: 40px;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            transition: border-color 0.3s ease;
        }

        .captcha-image:hover {
            border-color: #667eea;
        }

    </style>
</head>
<body>
<div class="login-container">
    <div class="login-header">
        <div class="logo">🎓</div>
        <div class="title">IVT绩效考核系统</div>
        <div class="subtitle">管理后台</div>
    </div>

    <form id="loginForm">
        <div class="error-message" id="errorMessage"></div>

        <div class="form-group">
            <label class="form-label" for="username">用户名</label>
            <input class="form-input" id="username" placeholder="请输入用户名" required type="text"
                   value="guanhuiliang">
        </div>

        <div class="form-group">
            <label class="form-label" for="password">密码</label>
            <input class="form-input" id="password" placeholder="请输入密码" required type="password" value="123">
        </div>

        <div class="form-group">
            <label class="form-label" for="captcha">验证码</label>
            <div class="captcha-container">
                <input class="form-input captcha-input" id="captcha" placeholder="请输入验证码" required type="text" maxlength="4">
                <img class="captcha-image" id="captchaImage" src="/evaluation/api/admin/captcha" alt="验证码" onclick="refreshCaptcha()">
            </div>
        </div>

        <button class="login-btn" id="loginBtn" type="submit">
            登录
        </button>
    </form>

    <div class="footer">
        © 2025 IVT绩效考核系统. All rights reserved.
    </div>
</div>

<script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const captcha = document.getElementById('captcha').value;

            if (!username || !password) {
                showError('请输入用户名和密码');
                return;
            }

            if (!captcha) {
                showError('请输入验证码');
                return;
            }

            const loginBtn = document.getElementById('loginBtn');
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';

            // 调用登录接口
            fetch('/evaluation/api/admin/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password,
                    captcha: captcha
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 保存登录状态到localStorage (可选，主要依靠session)
                    localStorage.setItem('adminLogin', 'true');
                    localStorage.setItem('adminUser', JSON.stringify({
                        username: username,
                        loginTime: new Date().toISOString()
                    }));

                    // 跳转到管理首页
                    window.location.href = 'index.html';
                } else {
                    showError(data.message || '登录失败');
                    refreshCaptcha(); // 登录失败时刷新验证码
                    document.getElementById('captcha').value = ''; // 清空验证码输入
                    loginBtn.disabled = false;
                    loginBtn.textContent = '登录';
                }
            })
            .catch(error => {
                console.error('登录请求失败:', error);
                showError('网络错误，请稍后重试');
                refreshCaptcha(); // 网络错误时也刷新验证码
                document.getElementById('captcha').value = ''; // 清空验证码输入
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
            });
        });

        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.textContent = message;
            errorDiv.classList.add('show');

            // 3秒后自动隐藏错误信息
            setTimeout(() => {
                errorDiv.classList.remove('show');
            }, 3000);
        }

        // 页面加载时检查是否已登录（使用后端API验证）
        document.addEventListener('DOMContentLoaded', function() {
            // 检查是否有来自其他页面的消息
            const loginMessage = sessionStorage.getItem('loginMessage');
            if (loginMessage) {
                showError(loginMessage);
                sessionStorage.removeItem('loginMessage');
            }

            checkAdminLoginStatus();
        });

        function checkAdminLoginStatus() {
            // 如果localStorage中没有登录标记，直接显示登录表单
            const isLoggedIn = localStorage.getItem('adminLogin');
            if (isLoggedIn !== 'true') {
                return;
            }

            // 如果localStorage中有登录标记，验证后端Session是否有效
            fetch('/evaluation/api/admin/check')
                .then(response => response.json())
                .then(data => {
                    if (data.success && data.code === 200) {
                        // 后端Session有效，跳转到管理页面
                        window.location.href = 'index.html';
                    } else {
                        // 后端Session无效，清除localStorage
                        localStorage.removeItem('adminLogin');
                        localStorage.removeItem('adminUser');
                    }
                })
                .catch(error => {
                    console.error('检查登录状态失败:', error);
                    // 网络错误时清除localStorage
                    localStorage.removeItem('adminLogin');
                    localStorage.removeItem('adminUser');
                });
        }

        // 刷新验证码
        function refreshCaptcha() {
            const captchaImage = document.getElementById('captchaImage');
            captchaImage.src = '/evaluation/api/admin/captcha?t=' + new Date().getTime();
        }

        // 页面加载完成后初始化验证码
        document.addEventListener('DOMContentLoaded', function() {
            refreshCaptcha();
        });

</script>
</body>
</html>
